<template>
  <div>
    <el-card class="box-card">
      <el-form :inline="true" class="demo-form-inline">
       <el-form-item label="查询处" >
          <el-input
            type="text"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd()">增加</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-table :data="tableData" style="width: 100%" border>
      <el-table-column label="id" width="180">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户名">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.username }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否启用">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.isActived }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.createdTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.row.remarks }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            @click.native.prevent="deleteRow(scope.row)"
            type="text"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="用户的信息" :visible.sync="dialogFormVisible">
      <el-form :model="formData">
        <el-form-item label="用户名" label-width="120px">
          <el-input v-model="formData.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="备注" label-width="120px ">
          <el-input v-model="formData.remarks" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancle">取 消</el-button>
        <el-button type="primary" @click="handleSave"
          >确 定</el-button
        >
      </div>
    </el-dialog>

    <div class="block">
      <span class="demonstration"></span>
      <el-pagination
        :current-page="pager.pageIndex"
        :page-size="pager.pageSize"
        layout="sizes, prev, pager, next, jumper,total"
        :total="pager.rowsTotal"
        @current-change="handlePageIndexChange"
        @size-change="handlePageSizeChange"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { DelId, getList, addUser, modUser } from "../api/users";
export default {
  data() {
    return {
      tableData: [],
      pager: {
        pageIndex: 1, // 页码
        pageSize: 10, // 页面大小（每页项目的数量）
        rowsTotal: 30, // 总记录数
      },
      formData: {
        id:0,
        username: "",
        password:"113",
        remarks: "",
      },
      currentIndex:0,
      dialogFormVisible: false,
    };
  },
  methods: {
    //增加
    handleAdd() {
      this.dialogFormVisible = true
      this.formData.id=0
      this.formData.username=''
      this.formData.remarks=''
        // console.log(this.formData)
    },
    //编辑
    handleEdit(index, row) {
      this.dialogFormVisible=true
      this.formData.id=row.id
      this.formData.username=row.username
      this.formData.remarks=row.remarks

      this.currentIndex=index
      // console.log(this.formData);
    },
    //取消
    handleCancle(){
       this.dialogFormVisible = false;
    },
     // 改变页码后，重新拉取数据
    handlePageIndexChange(val) {
      this.pager.pageIndex = val;
      this.fetchData(this.pager);
    },
    // 改变页大小后，重新拉取数据
    handlePageSizeChange(val) {
      this.pager.pageSize = val;
      this.fetchData(this.pager);
    },
    // 拉取数据方法
    fetchData(pager) {
      getList(pager)
        .then(({ data }) => {
          let res = data.data;
          this.tableData = res.data;
          this.pager = res.pager;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //确定
    handleSave(){
      this.dialogFormVisible = false;
      if(this.formData.id===0){
        addUser(this.formData).then(({data})=>{
          console.log(data);
          this.tableData.push(data.data);
        })
      }else{
        modUser(this.formData.id,this.formData).then(({data})=>{
          console.log(data);
          this.tableData.splice(this.currentIndex,1,data.data)
        })
      }
      console.log(this.formData);
    },

    //删除按钮
    deleteRow(row) {
      location.reload();
      DelId(row.id).then(() => {
        this.getUsers;
      });
    },
    getUsers() {
      getList(this.pager)
        .then(({ data }) => {
          console.log(data);
          let res = data.data;
          this.tableData = res.data;
          console.log(res.pager);
          this.pager = res.pager;
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },
  mounted() {
     this.fetchData(this.pager);
    this.getUsers();

    getList(this.pager)
      .then(({ data }) => {
        console.log(data);
        let res = data.data;
        this.tableData = res.data;
        this.pager = res.pager;
      })
      .catch((err) => {
        console.log(err);
      });
  },
};
</script>